<template>
	<view>
		桌位： {{ tableSeat }}
		<button @tap="handleLogin">授权登录</button>
	</view>
</template>

<script>
	import { LOGIN, byUserId_OrderDetail, byUserIdMessage } from '@/common/http.api.js'
	export default {
		data() {
			return {
				tableSeat: '',
			}
		},
		onLoad(option) {
			this.tableSeat = option.tableSeat
			if (this.$store.state.userId) {
				uni.navigateTo({
					url: `/pagesA/home/scanning/scanning?tableSeat=${this.tableSeat}`
				}) 
			}
		},
		methods: {
			handleLogin() {
				uni.getUserProfile({
					desc: "获取信息用于登录",
					success: (userProfileRes) => {
						console.log(userProfileRes, 'userProfileRes')
						uni.login({
							success: (loginRes) => {
								// console.log(loginRes);
								const code = loginRes.code
								const secret = '66f401760c3b9b9e6d4ab320fc301785'
								const appid = 'wx2b7223dc574f8ec6'
								// 这个接口用于校验code
								const url = `https://api.weixin.qq.com/sns/jscode2session?appid=${appid}&secret=${secret}&js_code=${code}&grant_type=authorization_code`
								uni.request({
									url: url,
									success: (res) => {
										// console.log(res, 'res')
										const user = {
											// 这里要的userInfo，因为用户没登录，this.userInfo的值是{}
											openid: res.data.openid,
											nickName: userProfileRes.userInfo.nickName,
											avatar: userProfileRes.userInfo.avatarUrl,
											sex: userProfileRes.userInfo.gender
										}
										// console.log(user, "user");
										// 拿到openid之后 通过登录接口 登录 把个人信息传给接口
										LOGIN(user).then( resp => {
											// console.log(resp, 'resp')
											// 从返回数据里拿到userId 并存到仓库和缓存
											this.$store.commit('SET_USERID', resp.data)
											uni.setStorageSync('scan-userId', resp.data)
											// // 从返回数据里拿到token 并存到仓库和缓存
											this.$store.commit('SET_TOKEN', resp.msg)
											uni.setStorageSync('scan-token', resp.msg)
											
											//获取个人信息
											byUserIdMessage( resp.data ).then( response => {
												this.$store.commit('SET_USERINFO', response.data)
												uni.setStorageSync('scan-userinfo', JSON.stringify(response.data))
											})
											byUserId_OrderDetail(resp.data).then( respo => {
												for (let i=0; i<respo.rows.length; i++) {
													if (respo.rows[i].currentStatus == '支付失败') {
														uni.showModal({
															title: '',
															content: '您有支付失败订单，请支付',
															confirmText: '去支付',
															success: respon => {
																if (respon.confirm) {
																	uni.navigateTo({
																		url: `/pagesA/home/closeAccount/closeAccount?orderId=${respo.rows[i].id}`
																	})
																}
															}
														})
														return
													}
												}
												uni.navigateTo({
													url: `/pagesA/home/scanning/scanning?tableSeat=${this.tableSeat}`
												}) 
											})
										})
									}
								})
							}
						})
					},
					fail: () => {
						uni.showToast({
							icon: "none",
							title: "已拒绝授权"
						})
					}
				})
			}
		}
	}
</script>

<style lang="less">

</style>
